<template>
  <div class="RecordsList">
    <NavBar fixed placeholder title="报修记录" z-index="999" ref="nav">
      <template #left>
        <Icon
          name="arrow-left"
          size="0.53rem"
          color="#333333"
          @click="$router.back()"
        />
      </template>
      <template #right>
        <Icon
          name="filter-o"
          size="0.53rem"
          color="#333333"
          @click="show = true"
        />
      </template>
    </NavBar>
    <!-- <Search
      v-model="search"
      background="#e1e1e1"
      placeholder="请输入搜索关键词"
      show-action
      @search="onSearch"
      @cancel="onCancel"
    /> -->
    <!-- 列表 -->
    <PullRefresh v-model="isLoading" @refresh="onRefresh">
      <List
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <div
          class="item"
          v-for="(item, i) in list"
          :key="i"
          @click="toJump(item)"
        >
          <div class="row">
            <div class="title flex">
              <div class="placeholder"></div>
              <span>报修编号</span>
              <span class="id">{{ item.ermId }}</span>
            </div>
            <div class="state">{{ item.state }}</div>
          </div>
          <div class="row row1">
            <div class="name">{{ item.equipName }}</div>
          </div>
          <div class="row2">
            <div class="cell">
              <span>报修人</span>
              <div class="type">{{ item.userName + " " + item.phone }}</div>
            </div>
            <div class="cell">
              <span>报修时间</span>
              <div class="type">{{ item.time }}</div>
            </div>
          </div>
        </div>
      </List>
    </PullRefresh>
    <!-- 菜单 -->
    <Popup
      class="Popup"
      v-model="show"
      position="right"
      @closed="closed"
      :style="{ height: '100%', width: '70%' }"
    >
      <div class="task">
        <div class="title">任务状态</div>
        <div class="state">
          <div
            class="state-item flex"
            v-for="(item, i) in stateList"
            :key="i"
            @click="item.state = !item.state"
          >
            <div class="btn flex" :class="{ change: item.state == true }">
              {{ item.name }}
            </div>
          </div>
        </div>
      </div>
      <div class="title">联系人</div>
      <input
        type="text"
        placeholder="请输入联系人"
        class="ipt"
        v-model="user"
      />
      <div class="title">联系电话</div>
      <input
        type="text"
        placeholder="请输入联系电话"
        class="ipt"
        v-model="phone"
      />
      <div class="title">报修时间</div>
      <div class="flex">
        <input
          type="text"
          placeholder="请选择开始时间"
          class="ipt1"
          v-model="startTime"
          readonly
          @click="showTime1 = true"
        />
        <span class="placeholder1 flex">一</span>
        <input
          type="text"
          placeholder="请选择结束时间"
          class="ipt1"
          v-model="endTime"
          readonly
          @click="showTime2 = true"
        />
      </div>

      <div class="btn-box flex">
        <Button class="reset" @click="reset">重置</Button>
        <Button class="ok" @click="ok">确定</Button>
      </div>
    </Popup>
    <Popup
      class="time"
      v-model="showTime1"
      position="bottom"
      :style="{ height: '50%' }"
    >
      <DatetimePicker
        v-model="currentDate"
        type="datetime"
        title="作业开始时间"
        :min-date="minDate"
        :max-date="maxDate"
        @confirm="startConfirm"
        @cancel="showTime1 = false"
        item-height="1rem"
      ></DatetimePicker>
    </Popup>
    <Popup
      class="time"
      v-model="showTime2"
      position="bottom"
      :style="{ height: '50%' }"
    >
      <DatetimePicker
        v-model="currentDate"
        type="datetime"
        title="作业结束时间"
        :min-date="minDate1"
        :max-date="maxDate1"
        @confirm="endConfirm"
        @cancel="showTime2 = false"
        item-height="1rem"
      ></DatetimePicker>
    </Popup>
  </div>
</template>

<script>
import {
  EquipmentRepairServicePost,
  EquipmentRepairServiceList,
} from "@/api/work";
import {
  Popup,
  NavBar,
  Icon,
  Field,
  Cell,
  List,
  Button,
  Search,
  DatetimePicker,
  PullRefresh,
} from "vant";
export default {
  props: {},
  data() {
    return {
      list: [
        // {
        //   id: "11111",
        //   title: "title",
        //   state: "待分配",
        //   type: "电梯",
        //   user: "小王",
        //   phone: "18254653231",
        //   date: "2021-01-01",
        //   time: "20:20:20",
        // },
        // {
        //   id: "11111",
        //   title: "title",
        //   state: "待分配",
        //   type: "电梯",
        //   user: "小王",
        //   phone: "18254653231",
        //   date: "2021-01-01",
        //   time: "20:20:20",
        // },
      ],
      loading: false,
      finished: false,
      show: false,
      stateList: [],
      stateList2: [],
      user: "",
      phone: "",
      startTime: "",
      endTime: "",
      search: "",
      pageNum: 1,
      pageSize: 10,
      showTime1: false,
      showTime2: false,
      minDate: new Date(2020, 0, 1),
      minDate1: new Date(2020, 0, 1),
      maxDate: new Date(2025, 10, 1),
      maxDate1: new Date(2025, 10, 1),
      currentDate: new Date(2021, 0, 17),
      isLoading: false,
    };
  },
  computed: {
    stateStr() {
      let arr = [];
      this.stateList.forEach((v) => {
        if (v.state) {
          arr.push(v.id);
        }
      });
      if (arr.length > 0) {
        return arr.join(",");
      }
      return "";
    },
  },
  created() {},
  mounted() {
    this.getMuneList();
  },
  watch: {},
  methods: {
    async getMuneList() {
      const { param } = await EquipmentRepairServicePost({
        code: "EquipRepairsManage-state",
      });
      console.log("menu", param.data);
      param.data.forEach((v) => {
        v.state = false;
      });
      this.stateList = param.data;
      this.stateList2 = JSON.parse(JSON.stringify(this.stateList));
    },
    async onLoad() {
      let query = {
        state: this.stateStr,
        userName: this.user,
        phone: this.phone,
        startTime: this.startTime,
        endTime: this.endTime,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        orgNo: window.localStorage.getItem("orgNo"),
      };
      const { param } = await EquipmentRepairServiceList(query);
      // console.log(param.data);
      this.list.push(...param.data);
      this.loading = false;
      console.log(33333);
      if (this.list.length < this.pageSize * this.pageNum) {
        this.finished = true;
      }
      this.pageNum++;
    },
    toJump(item) {
      // return console.log(item);
      this.$router.push({
        path: "Details1",
        query: {
          ermNo: item.ermNo,
        },
      });
    },
    reset() {
      this.stateList.forEach((v) => {
        v.state = false;
      });
      this.user = "";
      this.phone = "";
      this.startTime = "";
      this.endTime = "";
    },
    onSearch() {},
    onCancel() {},
    ok() {
      this.list = [];
      this.pageNum = 1;
      this.onLoad();
      this.stateList2 = JSON.parse(JSON.stringify(this.stateList));

      this.show = false;
    },
    startConfirm(value) {
      console.log(this.getTime(value));
      this.startTime = this.getTime(value);
      this.minDate1 = value;
      this.showTime1 = false;
    },
    endConfirm(value) {
      console.log(this.getTime(value));
      this.endTime = this.getTime(value);
      this.maxDate = value;
      this.showTime2 = false;
    },
    getTime(time) {
      const year = time.getFullYear();
      const Month = time.getMonth() + 1;
      const Date = time.getDate();
      let hour = time.getHours();
      hour = hour < 10 ? "0" + hour : hour;
      let m = time.getMinutes();
      m = m < 10 ? "0" + m : m;
      return `${year}-${Month}-${Date} ${hour}:${m}`;
    },
    closed() {
      this.stateList = JSON.parse(JSON.stringify(this.stateList2));
    },
    async onRefresh() {
      this.pageNum = 1;
      this.finished = false;
      this.list = [];
      await this.onLoad();
      this.isLoading = false;
    },
  },
  components: {
    NavBar,
    Button,
    Popup,
    Icon,
    Field,
    List,
    Cell,
    Search,
    DatetimePicker,
    PullRefresh,
  },
};
</script>

<style scoped lang="less">
@import "~@/style/var.less";
.RecordsList {
  .flex {
    display: flex;
    justify-content: center;
    align-items: center;
  }
  background-color: #f7f7f9;
  // /deep/.van-nav-bar__content {
  //   background-color: #3982f6;

  //   .van-nav-bar__title {
  //     color: #fff;
  //   }
  // }
  .item {
    border-top: 1px solid #eee;
    margin-top: 20px;
    background-color: #fff;
    .row {
      display: flex;
      align-items: center;
      justify-content: space-between;
      height: 72px;
      border-bottom: 1px solid #eee;

      .title {
        font-size: 32px;
        font-weight: 700;
        .placeholder {
          width: 5px;
          height: 30px;
          background: @primary-linear;
          margin: 0 20px;
        }
        .id {
          margin-left: 20px;
          font-weight: 500;
        }
      }
      .state {
        margin-right: 20px;
        width: 132px;
        height: 44px;
        // background-color: #dcf4e4;
        border-radius: 22px;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 28px;
        // color: #04b33e;
        color: @primary;
        font-family: PingFang SC;
        font-weight: 500;
      }
    }
    .row1 {
      justify-content: start;
      font-size: 28px;
      color: #999999;
      padding: 0 30px;
      .icon {
        margin-right: 0 20px;
        font-size: 35px;
      }
      .name {
        font-size: 28px;
        font-family: PingFang SC;
        font-weight: 500;
        color: #696d73;
      }
    }
    .row2 {
      height: 144px;
      display: flex;
      flex-flow: column;
      font-size: 28px;
      color: #999999;
      padding: 0 30px;
      .cell {
        flex: 1;
        display: flex;
        justify-content: space-between;
        align-items: center;
      }
    }
  }
  .Popup {
    .task {
      border-bottom: 1px solid #a7a7a7;
    }
    .title {
      padding: 15px;
      font-size: 28px;
      color: #a7a7a7;
    }
    .state {
      display: flex;
      //   justify-content: space-between;
      flex-direction: row;
      font-size: 26px;
      color: #6e6e6e;
      flex-wrap: wrap;

      .state-item {
        width: 33%;
        height: 74px;
        .btn {
          position: relative;
          width: 157px;
          height: 58px;
          background-color: #f6f6f6;
          border: 0;
          border-radius: 4px;
          color: #6e6e6e;
          &.change {
            border: 3px solid @primary;
            background: #fff;
          }
          &.change::after {
            content: "\F0C8";
            height: 35px;
            font-family: "vant-icon";
            text-indent: 16px;
            line-height: 1.7;
            background-repeat: no-repeat;
            position: absolute;
            bottom: -2px;
            right: -2px;
            color: #fff;
            background-image: linear-gradient(
              146deg,
              transparent 0%,
              transparent 50%,
              @primary 50%,
              @primary 100%
            );
          }
        }
      }
    }
    .ipt {
      width: 90%;
      height: 58px;
      text-indent: 1em;
      font-size: 24px;
      margin-left: 20px;
      background-color: #f6f6f6;
    }
    .ipt1 {
      width: 40%;
      height: 58px;
      text-indent: 1em;
      font-size: 24px;

      background-color: #f6f6f6;
    }
    .placeholder1 {
      width: 60px;
      height: 58px;
      color: #3982f6;
    }
    .btn-box {
      position: absolute;
      bottom: 0;
      width: 100%;
      margin-bottom: 40px;
      .ok {
        width: 157px;
        height: 58px;
        border: 1px solid @primary;
        border-radius: 4px;
        color: @primary;
        margin-left: 50px;
      }
      .reset {
        width: 157px;
        height: 58px;
        // background-color: #dddddd;
        border-radius: 4px;
        color: #6e6e6e;
      }
    }
  }
}
</style>
